//
//  SpreadCollectionView.m
//  B
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "SpreadCollectionView.h"
#import "ChannelCollectionViewCell.h"

@interface SpreadCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate>

// collectionView --> Array
@property (nonatomic, strong) NSArray *collectionArray;
// collectionView --> imageArray
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation SpreadCollectionView

-(NSArray *)collectionArray
{
    if (!_collectionArray) {
        _collectionArray = [NSArray arrayWithObjects:@"英超", @"西甲", @"德甲", @"意甲", @"法甲", @"中超", nil];
    }
    return _collectionArray;
}
-(NSArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = [NSArray arrayWithObjects:@"EPL_logo", @"LFP_logo", @"Bundesliga_logo", @"SERIE_logo", @"CFL_logo", @"CSL_logo", nil];
    }
    return _imageArray;
}

// 创建collectionView
-(void)createCollectionView
{
    self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight / 2);
    self.backgroundColor = [UIColor redColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.itemSize = CGSizeMake(110, 150);
    
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight / 2) collectionViewLayout:layout];
    [self addSubview:_mainCollectionView];
    _mainCollectionView.backgroundColor = [UIColor whiteColor];
    _mainCollectionView.alpha = 0.98;
    
    [_mainCollectionView registerNib:[UINib nibWithNibName:@"ChannelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collectionCell"];
    // 代理
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
}

// 隐藏collectionView
-(void)hiddenCollectionView
{
    [self removeFromSuperview];
}

// collectionView 中 item 点击方法
-(void)collectionItemClick:(UIButton *)button
{
    if (button.tag >= 10001 && button.tag <= 10006) {
        long a = button.tag - 10000;
        _getCollectionTitleBT = self.collectionArray[a];
    } else {
        long a = button.tag - 20000;
        _getCollectionTitleBT = self.collectionArray[a];
    }
    // 通知传值
    // 监听者
    [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:_getCollectionTitleBT];
    [self hiddenCollectionView];
}

#pragma mark - 代理
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    [cell.selectButton setTitle:self.collectionArray[indexPath.row] forState:UIControlStateNormal];
    cell.selectButton.tag = 10000 + indexPath.row;
    [cell.selectButton addTarget:self action:@selector(collectionItemClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.imageButton setBackgroundImage:[UIImage imageNamed:self.imageArray[indexPath.row]] forState:UIControlStateNormal];
    [cell.imageButton addTarget:self action:@selector(collectionItemClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.imageButton.tag = 20000 + indexPath.row;
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
